package io.bsly.modules.app.service.sourceDb.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bsly.common.utils.PageUtils;
import io.bsly.modules.app.dao.sourceDao.HotelDao;
import io.bsly.modules.app.entity.sourceEntity.*;
import io.bsly.modules.app.pojo.HotelVo;
import io.bsly.modules.app.pojo.RoomTypeFrom;
import io.bsly.modules.app.service.sourceDb.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DS("slave")
@Service("hotelService")
public class HotelServiceImpl extends ServiceImpl<HotelDao, HotelEntity> implements HotelService {

    @Autowired
    private RoomStatusService roomStatusService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private CanalsRoomService canalsRoomService;
    @Autowired
    private RoomTypeService roomTypeService;


    @Override
    public PageUtils queryPage(HotelVo hotelVo) {
        QueryWrapper<HotelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(hotelVo.getName()),HotelEntity::getName,hotelVo.getName())
                .like(StringUtils.isNotBlank(hotelVo.getAddress()),HotelEntity::getAddress,hotelVo.getAddress())
                .eq(StringUtils.isNotBlank(hotelVo.getProvince()),HotelEntity::getProvince,hotelVo.getProvince())
                .eq(StringUtils.isNotBlank(hotelVo.getCity()),HotelEntity::getCity,hotelVo.getCity())
                .eq(!ObjectUtil.isNull(hotelVo.getStar()),HotelEntity::getStar,hotelVo.getStar());

        Page<HotelEntity> pageList = this.page(new Page<>(hotelVo.getPage(), hotelVo.getLimit()), queryWrapper);

        return new PageUtils(pageList);
    }

    @Override
    public List<CanalsRoomEntity> listRoom(RoomTypeFrom from) {
//        List<RoomTypeEntity> roomTypeEntities = roomTypeService.listByMeal(from);
//        List<Integer> roomTypeIdList = roomTypeEntities.stream().map(one -> one.getId()).collect(Collectors.toList());

        List<RoomEntity> roomList = roomService.list(new LambdaQueryWrapper<RoomEntity>()
                .eq(RoomEntity::getHotelId, from.getHotelId())
                .in(RoomEntity::getMeal, from.getMeals())
                .ne(RoomEntity::getStatus, '0')
        );
        if (CollectionUtils.isEmpty(roomList)){
            return null;
        }
        List<Integer> roomIds = roomList.stream().map(one -> one.getId()).collect(Collectors.toList());

        LocalDate InDate = LocalDate.parse(from.getInDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate OutDateTemp = LocalDate.parse(from.getOutDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate OutDate = OutDateTemp.minusDays(1);

        List<RoomStatusEntity> roomStatusList = roomStatusService.list(new LambdaQueryWrapper<RoomStatusEntity>()
                .in(RoomStatusEntity::getRoomId, roomIds)
                .apply("date_format(date,'%Y-%m-%d') >= {0}", InDate)
                .apply("date_format(date,'%Y-%m-%d') <= {0}", OutDate)
                .eq(RoomStatusEntity::getChannel, "ctrip")
                .gt(RoomStatusEntity::getNum, 0)
                .ne(RoomStatusEntity::getStatus,'0')//0 下架  1 和空都是正常状态
        );

        if (CollectionUtils.isEmpty(roomStatusList)){
            return null;
        }

        roomIds = roomStatusList.stream().map(one -> one.getRoomId()).collect(Collectors.toList());
        List<CanalsRoomEntity> list = canalsRoomService.list(new LambdaQueryWrapper<CanalsRoomEntity>()
                .in(CanalsRoomEntity::getLocalRoomId, roomIds)
                .ne(CanalsRoomEntity::getStatus, 0)//0 下架  1 和空都是正常状态
                .select(CanalsRoomEntity::getId,CanalsRoomEntity::getLocalRoomId,CanalsRoomEntity::getName)
        );
        return list;
    }


}